﻿using EPE.DBUtility;
using EPE.IDAL;
using EPE.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace EPE.MySqlDAL
{
    public class ShopsImpl : IShops
    {
        public DataSet GetListByPage(string strProjectName, string strBuildName, string strShopsName, string companyid, string orderby, int pageIndex, int dataCount)
        {
            string strSQL;

            DataSet retDS = new DataSet();

            DataBaseTool clsDbTool = new DataBaseTool();

            try
            {
                strSQL = " SELECT S.shopsid,S.shopsname,S.area,S.located,S.property,B.buildid,B.projectid,B.buildname,p.projectname ";
                strSQL = strSQL + " FROM tbl_Shops S,tbl_ShopsBuild B,tbl_ShopsProject P ";
                strSQL = strSQL + " WHERE 1=1 ";
                strSQL = strSQL + " AND S.buildid=B.buildid ";
                strSQL = strSQL + " AND B.projectid=P.projectid ";
                strSQL = strSQL + " AND P.companyid=@companyid ";

                if (!string.IsNullOrEmpty(strProjectName))
                {
                    strSQL = strSQL + " AND projectname=@projectname ";
                }
                if (!string.IsNullOrEmpty(strBuildName))
                {
                    strSQL = strSQL + " AND buildname=@buildname ";
                }
                if (!string.IsNullOrEmpty(strShopsName))
                {
                    strSQL = strSQL + " AND shopsname=@shopsname ";
                }
                clsDbTool.ParameterClear();
                clsDbTool.ParameterAdd("projectname", strProjectName);
                clsDbTool.ParameterAdd("buildname", strBuildName);
                clsDbTool.ParameterAdd("shopsname", strShopsName);
                clsDbTool.ParameterAdd("companyid", companyid);
                clsDbTool.CommandText = Pagination.PagingDataView(strSQL, pageIndex, dataCount, orderby);
                clsDbTool.Open();

                retDS = clsDbTool.GetDataSet();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                clsDbTool.Close();
            }
            return retDS;
        }
        public void Insert(CShops model)
        {
            string strSQL;

            int ret;

            DataBaseTool clsDbTool = new DataBaseTool();

            try
            {
                strSQL = " INSERT INTO tbl_shops (  ";
                strSQL = strSQL + "buildid ,";
                strSQL = strSQL + "shopsname ,";
                strSQL = strSQL + "area ,";
                strSQL = strSQL + "located ,";
                strSQL = strSQL + "property ,";
                strSQL = strSQL + "paraid )";

                strSQL = strSQL + "VALUES (@buildid ,";
                strSQL = strSQL + "@shopsname ,";
                strSQL = strSQL + "@area ,";
                strSQL = strSQL + "@located ,";
                strSQL = strSQL + "@property ,";
                strSQL = strSQL + "@paraid )";




                clsDbTool.ParameterClear();
                clsDbTool.ParameterAdd("buildid", model.buildid);
                clsDbTool.ParameterAdd("shopsname", model.shopsname);
                clsDbTool.ParameterAdd("area", model.area);
            
                clsDbTool.ParameterAdd("located", model.located);
                clsDbTool.ParameterAdd("property", model.property);
                clsDbTool.ParameterAdd("paraid", model.paraid);
                

                //clsDbTool.SequenceText = "tbl_shopsproject";
                //clsDbTool.TableText = "tbl_shopsproject";
                clsDbTool.CommandText = strSQL;
                clsDbTool.Open();
                clsDbTool.BeginTransaction();
                clsDbTool.ExecuteScalar();

                clsDbTool.Commit();

            }
            catch (Exception ex)
            {
                clsDbTool.Rollback();
                throw ex;
            }
            finally
            {
                clsDbTool.Close();
            }
        }
        public void Update(CShops model) 
        {
            string strSQL;


            DataBaseTool clsDbTool = new DataBaseTool();

            try
            {
                strSQL = "update tbl_shops set ";

                strSQL = strSQL + "buildid = @buildid,";
                strSQL = strSQL + "shopsname = @shopsname,";
                strSQL = strSQL + "area = @area,";
                strSQL = strSQL + "located = @located,";
                strSQL = strSQL + "property = @property,";
                strSQL = strSQL + "paraid = @paraid";

                strSQL = strSQL + " WHERE 1=1";
                strSQL = strSQL + " AND shopsid=@shopsid";


                clsDbTool.ParameterClear();
                clsDbTool.ParameterAdd("shopsid", model.shopsid);
                clsDbTool.ParameterAdd("buildid", model.buildid);
                clsDbTool.ParameterAdd("shopsname", model.shopsname);
                clsDbTool.ParameterAdd("area", model.area);
                clsDbTool.ParameterAdd("located", model.located);
                clsDbTool.ParameterAdd("property", model.property);
                clsDbTool.ParameterAdd("paraid", model.paraid);

                clsDbTool.CommandText = strSQL;

                clsDbTool.Open();
                clsDbTool.BeginTransaction();

                clsDbTool.ExecuteScalar();

                clsDbTool.Commit();

            }
            catch (Exception ex)
            {
                clsDbTool.Rollback();
                throw ex;
            }
            finally
            {
                clsDbTool.Close();
            }
        }

        public DataSet GetListByUpdate(string strId)
        {
            string strSQL;

            DataSet retDS = new DataSet();

            DataBaseTool clsDbTool = new DataBaseTool();

            try
            {
                strSQL = " SELECT S.shopsid,S.shopsname,S.area,S.located,S.property,S.paraid,B.buildid,B.projectid,B.buildname,p.projectname ";
                strSQL = strSQL + " FROM tbl_Shops S,tbl_ShopsBuild B,tbl_ShopsProject P ";
                strSQL = strSQL + " WHERE 1=1 ";
                strSQL = strSQL + " AND S.buildid=B.buildid ";
                strSQL = strSQL + " AND B.projectid=P.projectid ";
                //strSQL = strSQL + " AND P.companyid=@companyid ";
                if (!string.IsNullOrEmpty(strId))
                {
                    strSQL = strSQL + " AND shopsid=@shopsid ";
                }


                clsDbTool.ParameterClear();
                clsDbTool.ParameterAdd("shopsid", strId);
                clsDbTool.CommandText = strSQL;
                clsDbTool.Open();

                retDS = clsDbTool.GetDataSet();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                clsDbTool.Close();
            }
            return retDS;
        }

    }
}
